package com.dnb.webmash.playmaker.client;

import com.dnb.webmash.playmaker.client.ui.AddMediaTab;
import com.dnb.webmash.playmaker.client.ui.MediaTypesTab;
import com.dnb.webmash.playmaker.client.ui.MyPlayerTab;
import com.dnb.webmash.playmaker.client.ui.WatchTab;
import com.dnb.webmash.playmaker.shared.LoginInfo;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.RootPanel;

public class PlayMaker implements EntryPoint {
	private static final AppConstants CONSTANTS = GWT.create(AppConstants.class);	
	final Button getChanButton = new Button(CONSTANTS.getChanButton_html());
	protected Boolean requestMediaOnly = true; //default to only media and quick request
	
	public void onModuleLoad() {

		final DecoratedTabPanel mainDecoratedTabPanel = new DecoratedTabPanel();
		RootPanel.get().add(mainDecoratedTabPanel);
		mainDecoratedTabPanel.setWidth(CONSTANTS.mainDecoratedTabPanel_width());
		final int windowHeight = Window.getClientHeight();
		mainDecoratedTabPanel.setHeight(windowHeight + "px");
		
		final WatchTab watchTab = new WatchTab();
		mainDecoratedTabPanel.add(watchTab, CONSTANTS.tab0_watchDockLayoutPanel_TabText(),false);
		watchTab.setSize("100%", windowHeight + "px");
		
		final MediaTypesTab mediaTypesTab = new MediaTypesTab();
		mediaTypesTab.setSize("100%", windowHeight + "px");
		
		//TODO fix and uncomment
		//final MyPlayerTab myPlayerTab = new MyPlayerTab();
		//mainDecoratedTabPanel.add(myPlayerTab, CONSTANTS.tab1_playerConfigDockLayoutPanel_TabText(),false);
		//myPlayerTab.setSize("100%", windowHeight + "px");
		
		final AddMediaTab addMediaTab = new AddMediaTab();
		mainDecoratedTabPanel.add(addMediaTab, CONSTANTS.tab5_addmediapanel_TabText(),false);
		addMediaTab.setSize("100%", windowHeight + "px");
		
		
		/*
		 * START OF IFRAME ABOUT AND CONFIG TABS
		 */
		
		//STATIC iFrame TABS CONTAINING ABOUT AND CONTACT PAGES
		final Frame tab3_aboutFrame = new Frame("About");
		tab3_aboutFrame.setUrl(CONSTANTS.tab3_aboutFrame_url());
		mainDecoratedTabPanel.add(tab3_aboutFrame, CONSTANTS.tab3_aboutFrame_TabText(), false);
		tab3_aboutFrame.setSize("100%", windowHeight + "px");
		
		final Frame tab4_contactFrame = new Frame("Contact");
		tab4_contactFrame.setUrl(CONSTANTS.tab4_contactFrame_url());
		mainDecoratedTabPanel.add(tab4_contactFrame, CONSTANTS.tab4_contactFrame_TabText(), false);
		tab4_contactFrame.setSize("100%", windowHeight + "px");			
		
		mainDecoratedTabPanel.selectTab(0);//select the first tab
		//**************************************** END OF TABS ***************************************************
		// Check login status using login service. and hide the tabs not authorized
		LoginServiceAsync loginService = GWT.create(LoginService.class);
		loginService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {
			@Override
			public void onFailure(Throwable caught) {
				//TODO log error: ("Unable to verify login status.");
			}
			@Override
			public void onSuccess(LoginInfo result) {
				if(result.isLoggedIn()) {
					if (result.isAdmin()){
						mainDecoratedTabPanel.add(mediaTypesTab, CONSTANTS.tab2_mediaTypesDockLayoutPanel_TabText(),false);
					} else{
						mainDecoratedTabPanel.remove(mediaTypesTab);
					}
				} else {
					mainDecoratedTabPanel.remove(mediaTypesTab);
				}
			}
		});
		
		// Track changes to window size
		Window.addResizeHandler(new ResizeHandler() {
			public void onResize(ResizeEvent event) {
				final int height = event.getHeight();
				mainDecoratedTabPanel.setHeight(height + "px");
				watchTab.setSize("100%", height + "px");
				mediaTypesTab.setSize("100%", height+"px");
				//myPlayerTab.setSize("100%", height+"px");
				addMediaTab.setSize("100%", height+"px");
				tab3_aboutFrame.setSize("100%", height + "px");
				tab4_contactFrame.setSize("100%", height + "px");				
				//TODO add all tabs here
			}
		});	
	}
}